home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / machserver / 1.098 / include / stdio.h < prev    next >
C/C++ Source or Header  |  1990-12-18  |  13KB  |  382 lines

  1. /*
  2.  * stdio.h --
  3.  *
  4.  *    This header file declares the stdio library facilities.  They
  5.  *    provide a general stream facility and routines for formatted
  6.  *    input and output.
  7.  *
  8.  * Copyright 1986, 1988 Regents of the University of California
  9.  * Permission to use, copy, modify, and distribute this
  10.  * software and its documentation for any purpose and without
  11.  * fee is hereby granted, provided that the above copyright
  12.  * notice appear in all copies.  The University of California
  13.  * makes no representations about the suitability of this
  14.  * software for any purpose.  It is provided "as is" without
  15.  * express or implied warranty.
  16.  *
  17.  * $Header: /sprite/src/lib/include/RCS/stdio.h,v 1.26 90/12/07 23:46:20 rab Exp $ SPRITE (Berkeley)
  18.  */
  19.  
  20. #ifndef _STDIO_H
  21. #define _STDIO_H
  22.  
  23. /* 
  24.  * sprite.h is needed for typedefs that are used in some function
  25.  * prototypes.  Unfortunately, some user programs define conflicting
  26.  * typedefs.  Because practically everyone uses stdio.h, we should
  27.  * give advance warning before forcing users to use the typedefs from
  28.  * sprite.h.  This must be done before we can turn on function
  29.  * prototypes for Sprite user program.  (Or, change the prototypes so 
  30.  * that they don't use the Sprite typedefs.)
  31.  */
  32. #include <cfuncproto.h>
  33.  
  34. #ifdef KERNEL
  35. #include <sprite.h>
  36. #endif
  37.  
  38. #ifndef EOF
  39. #define EOF (-1)
  40. #endif
  41.  
  42. #ifndef NULL
  43. #define NULL 0
  44. #endif
  45.  
  46. #ifndef _CLIENTDATA
  47. typedef int *ClientData;
  48. #define _CLIENTDATA
  49. #endif
  50.  
  51. #ifndef _VA_LIST
  52. #define _VA_LIST
  53. typedef char *va_list;
  54. #endif
  55.  
  56. /*
  57.  * The main data structure used by the stdio module is a FILE.  This
  58.  * describes a byte-sequential communication channel.  The channel
  59.  * includes buffer storage and the names of three stream-dependent
  60.  * procedures:
  61.  *
  62.  * The procedure readProc is called when another byte of data is needed
  63.  * and the buffer is empty (readCount == 0).  It should read more data
  64.  * into the buffer, reset readCount and lastAccess, and set the STDIO_EOF
  65.  * flag and/or status field if any problem occurred while reading the data.
  66.  *
  67.  * The writeProc procedure is similar to readProc, except that it is
  68.  * called when the buffer has filled (writeCount just became zero);
  69.  * its job is to write out the contents of the buffer and reset
  70.  * lastAccess and writeCount.  If the flush parameter is non-zero, then
  71.  * the procedure is being called as part of fflush, and it MUST empty
  72.  * the buffer.  Otherwise, the procedure may, if it chooses, increase
  73.  * the size of the buffer and return without actually writing anything.
  74.  *
  75.  * The third procedure, closeProc, is called when the stream is closed
  76.  * (writeProc is also called on close, before closeProc).  CloseProc
  77.  * should take any client-specific closing actions, such as closing
  78.  * the file corresponding to the stream or freeing the buffer space
  79.  * for the stream.  Its return value will be the return value from
  80.  * the fclose call.  If an error occurs while closing the stream, then
  81.  * the FILE structure should not be de-allocated, since the client will
  82.  * need to get at information in it to find out what went wrong.
  83.  *
  84.  * The procedures have the following calling sequences:
  85.  *
  86.  *    void readProc(stream)
  87.  *        FILE *stream;
  88.  *    {
  89.  *    }
  90.  
  91.  *    void writeProc(stream, flush)
  92.  *        FILE *stream;
  93.  *        Boolean flush;
  94.  *    {
  95.  *    }
  96.  
  97.  *    int closeProc(stream)
  98.  *        FILE *stream;
  99.  *    {
  100.  *    }
  101.  *
  102.  * See StdIoFileReadProc, StdIoFileWriteProc, and StdIoFileCloseProc for
  103.  * examples of these procedures.
  104.  */
  105.  
  106. typedef struct _file {
  107.     unsigned char *lastAccess;    /* Place (in buffer) from which last input
  108.                  * or output byte was read or written
  109.                  * (respectively). */
  110.     int readCount;        /* # of characters that may be read from
  111.                  * buffer before calling readProc to refill
  112.                  * the buffer. */
  113.     int writeCount;        /* # of characters that may be written into
  114.                  * buffer before calling writeProc to empty
  115.                  * the buffer.  WriteProc is called immediately
  116.                  * when the buffer fills, so that this value
  117.                  * is never zero unless the stream is not
  118.                  * currently being used for writing. */
  119.     unsigned char *buffer;    /* Pointer to storage for characters.  NULL
  120.                  * means storage hasn't been allocated yet. */
  121.     int bufSize;        /* Total number of bytes of storage available
  122.                  * in buffer. 0 means storage for buffer hasn't
  123.                  * been allocated yet. */
  124.     void (*readProc)_ARGS_((struct _file *));
  125.                 /* Procedure called to refill buffer. */
  126.     void (*writeProc)_ARGS_((struct _file *, Boolean));
  127.                 /* Procedure called to empty buffer. */
  128.     int (*closeProc)_ARGS_((struct _file *));
  129.                 /* Procedure called to close stream.  NULL
  130.                  * means no procedure to call. */
  131.     ClientData clientData;    /* Additional data for the use of the
  132.                  * procedures above,  e.g. the stream ID used
  133.                  * in kernel calls. */
  134.     int status;            /* Non-zero means an error has occurred while
  135.                  * emptying or filling the buffer.  This field
  136.                  * is set by readProc and writeProc. */
  137.     int flags;            /* Miscellaneous flags.  See below for values.
  138.                  */
  139.     struct _file *nextPtr;    /* For file streams, this is used to link all
  140.                  * file streams together (NULL means end of
  141.                  * list).  For other types of streams, it can
  142.                  * be used for anything desired by the
  143.                  * stream implementor. */
  144. } FILE;
  145.  
  146. /* Flags for FILEs:
  147.  *
  148.  * STDIO_READ:        Means that this stream is used for input.
  149.  * STDIO_WRITE:        Means that this stream is used for output.
  150.  * STDIO_EOF:        Means that an end-of-file has been encountered
  151.  *            on this stream.  All future reads will fail.
  152.  * STDIO_LINEBUF:    Means that this stream is line-buffered:  flush when
  153.  *            a newline is output or stdin is read.
  154.  * STDIO_NOT_OUR_BUF:      Means that the buffer for the stream belongs to someone
  155.  *                  else and should not be freed by the stdio library.
  156.  * 
  157.  */
  158.  
  159. #define STDIO_READ        1
  160. #define STDIO_WRITE        2
  161. #define STDIO_EOF        4
  162. #define STDIO_LINEBUF        8
  163. #define STDIO_NOT_OUR_BUF    16
  164.  
  165. /*
  166.  *----------------------------------------------------------------------
  167.  *
  168.  * getc --
  169.  * getchar --
  170.  * putc --
  171.  * putchar --
  172.  *
  173.  *    These four macros are used to input the next character from
  174.  *    a FILE or output the next character to a FILE.  Normally they
  175.  *    just move a character to or from a buffer, but if the buffer is
  176.  *    full (or empty) then they call a slow procedure to empty (or fill)
  177.  *    the buffer.
  178.  *
  179.  *    These macros are somewhat gross. putc is a ternary operator
  180.  *    to allow people to say things like
  181.  *
  182.  *        if (a)
  183.  *              putc(stdout, '\n');
  184.  *        else ...
  185.  *
  186.  *    If it were a complex expression, the compiler would complain.
  187.  *
  188.  * Results:
  189.  *    None.
  190.  *
  191.  * Side effects:
  192.  *    Information is modified in stream's buffer.
  193.  *
  194.  *----------------------------------------------------------------------
  195.  */
  196.  
  197. #ifndef lint
  198. #define getc(stream)                             \
  199.     (((stream)->readCount <= 0) ?                                       \
  200.         fgetc(stream) :                        \
  201.         ((stream)->readCount -= 1,                             \
  202.          (stream)->lastAccess += 1,                        \
  203.          *((stream)->lastAccess)))
  204.  
  205. #define putc(c, stream)                                                \
  206.     ((((stream)->writeCount <= 1) || ((stream)->flags & STDIO_LINEBUF)) ? \
  207.         fputc(c, stream) :                                  \
  208.         ((stream)->writeCount -= 1,                                    \
  209.          (stream)->lastAccess += 1,                                    \
  210.          *(stream)->lastAccess = c))
  211. #else
  212. _EXTERN int getc _ARGS_((FILE stream));
  213. _EXTERN int putc _ARGS_((int c, FILE stream));
  214. #endif
  215.  
  216. #define getchar() getc(stdin)
  217.  
  218. #define putchar(c) putc(c, stdout)
  219.  
  220. /*
  221.  *----------------------------------------------------------------------
  222.  *
  223.  * ferror --
  224.  * feof --
  225.  *
  226.  *    These two macros return information about whether an error
  227.  *    or end-of-file condition has occurred on a stream.
  228.  *
  229.  * Results:
  230.  *    ferror returns 0 if no error has occurred on the stream;
  231.  *    if an error has occurred then it returns the error code.
  232.  *    feof returns 0 if no end-of-file has been encountered on
  233.  *    the stream, and TRUE (non-zero) if an end-of-file has been
  234.  *    encountered.
  235.  *
  236.  * Side effects:
  237.  *    None.
  238.  *
  239.  *----------------------------------------------------------------------
  240.  */
  241.  
  242. #define ferror(stream) ((stream)->status)
  243. #define feof(stream) ((stream)->flags & STDIO_EOF)
  244.  
  245.  
  246. /*
  247.  *----------------------------------------------------------------------
  248.  *
  249.  * Miscellaneous additional things exported by stdio:
  250.  *
  251.  *----------------------------------------------------------------------
  252.  */
  253.  
  254. /*
  255.  * Handles for standard input and output channels.
  256.  */
  257.  
  258. extern FILE stdioInFile, stdioOutFile, stdioErrFile;
  259. #define stdin    (&stdioInFile)
  260. #define stdout    (&stdioOutFile)
  261. #define stderr    (&stdioErrFile)
  262.  
  263. /*
  264.  * Default buffer size:
  265.  */
  266.  
  267. #define BUFSIZ            4096
  268.  
  269. /*
  270.  * Flags to setvbuf:
  271.  */
  272.  
  273. #define _IOFBF        1
  274. #define _IOLBF        2
  275. #define _IONBF        3
  276.  
  277. /*
  278.  * Relative position indicator for fseek:
  279.  */
  280.  
  281. #define SEEK_SET    0
  282. #define SEEK_CUR    1
  283. #define SEEK_END    2
  284.  
  285. /*
  286.  *----------------------------------------------------------------------
  287.  *
  288.  * Procedures exported by the stdio module:
  289.  * (Note that these declarations are missing the "const" modifiers
  290.  * found in the ANSI version...)
  291.  * 
  292.  *----------------------------------------------------------------------
  293.  */
  294.  
  295. _EXTERN void    clearerr _ARGS_((FILE *stream));
  296. _EXTERN int    fclose _ARGS_((FILE *stream));
  297. _EXTERN FILE *    fdopen _ARGS_((int streamID, char *access));
  298. _EXTERN int    fflush _ARGS_((FILE *stream));
  299. _EXTERN int    fgetc _ARGS_((FILE *stream));
  300. _EXTERN char *    fgets _ARGS_((char *bufferPtr, int maxChars, FILE *stream));
  301. _EXTERN int    fileno _ARGS_((FILE *stream));
  302. _EXTERN FILE *    fopen _ARGS_((_CONST char *fileName, _CONST char *access));
  303. _EXTERN int    fputc _ARGS_((int c, FILE *stream));
  304. _EXTERN int    fputs _ARGS_((char *string, FILE *stream));
  305. _EXTERN int    fread _ARGS_((char *bufferPtr, int size, int numItems,
  306.                   FILE *stream));
  307. _EXTERN FILE *    freopen _ARGS_((_CONST char *fileName,
  308.                                 _CONST char *access, FILE *stream));
  309. _EXTERN long    fseek _ARGS_((FILE *stream, int offset, int base));
  310. _EXTERN long    ftell _ARGS_((FILE *stream));
  311. _EXTERN int    fwrite _ARGS_((char *bufferPtr, int size, int numItems,
  312.                    FILE *stream));
  313. _EXTERN char *    gets _ARGS_((char *bufferPtr));
  314. _EXTERN int    getw _ARGS_((FILE *stream));
  315. _EXTERN void    perror _ARGS_((_CONST char *msg));
  316. _EXTERN FILE *    popen _ARGS_((_CONST char *cmd, char *mode));
  317. _EXTERN int    pclose _ARGS_((FILE *ptr));
  318. _EXTERN int      remove _ARGS_((_CONST char *filename));
  319. _EXTERN int      rename _ARGS_((_CONST char *oldname, _CONST char *newname));
  320.  
  321. #ifdef KERNEL
  322. /*
  323.  * Special-case declarations for kernels:
  324.  * Printf returns void because the old Sys_Printf did.
  325.  * Varargs declarations aren't easy to do across all machines, so 
  326.  * we'll punt on them for now.
  327.  */
  328. _EXTERN void    printf _ARGS_(());
  329. _EXTERN int    fprintf _ARGS_(());
  330. _EXTERN int    scanf _ARGS_(());
  331. _EXTERN char *    sprintf _ARGS_(());
  332. _EXTERN int    sscanf _ARGS_(());
  333. _EXTERN int    fscanf _ARGS_(());
  334. _EXTERN int    vfprintf _ARGS_(());
  335. _EXTERN int    vfscanf _ARGS_(());
  336. _EXTERN int    vprintf _ARGS_(());
  337. _EXTERN char *    vsprintf _ARGS_(());
  338. #else /* KERNEL */
  339. /* 
  340.  * User-mode declarations for the routines in the special-case section:
  341.  * Note that the prototype declarations are actually no-ops until 
  342.  * _ARGS_ is turned on for user code.  Also, the varargs declarations 
  343.  * are only a first cut; there no guarantee they'll actually work when 
  344.  * _ARGS_ is turned on.
  345.  */
  346. _EXTERN int    printf _ARGS_((_CONST char *format, ...));
  347. _EXTERN int    fprintf _ARGS_((FILE *stream, _CONST char *format, ...));
  348. _EXTERN int    scanf _ARGS_((_CONST char *format, ...));
  349. _EXTERN char *    sprintf _ARGS_((char *s, _CONST char *format, ...));
  350. _EXTERN int    sscanf _ARGS_((char *s, _CONST char *format, ...));
  351. _EXTERN int    fscanf _ARGS_((FILE *stream, _CONST char *format, ...));
  352. _EXTERN int    vfprintf _ARGS_((FILE *stream,
  353.                                  _CONST char *format, va_list args));
  354. _EXTERN int    vfscanf _ARGS_((FILE *stream,
  355.                                 _CONST char *format, va_list args));
  356. _EXTERN int    vprintf _ARGS_((_CONST char *format, va_list args));
  357. _EXTERN char *    vsprintf _ARGS_((char *string,
  358.                                  _CONST char *format, va_list args));
  359. #endif /* KERNEL */
  360.  
  361. _EXTERN int    puts _ARGS_((_CONST char *string));
  362. _EXTERN int    putw _ARGS_((int w, FILE *stream));
  363. _EXTERN void    rewind _ARGS_((FILE *stream));
  364. _EXTERN void    setbuf _ARGS_((FILE *stream, char *buf));
  365. _EXTERN void    setbuffer _ARGS_((FILE *stream, char *buf, int size));
  366. _EXTERN void    setlinebuf _ARGS_((FILE *stream));
  367. _EXTERN int    setvbuf _ARGS_((FILE *stream, char *buf, int mode, int size));
  368. _EXTERN FILE *    tmpfile _ARGS_((void));
  369. _EXTERN char *    tmpnam _ARGS_((char *s));
  370. _EXTERN char *    tempnam _ARGS_((char *dir, char *pfx));
  371. _EXTERN int    ungetc _ARGS_((int c, FILE *stream));
  372. _EXTERN void    _cleanup _ARGS_((void));
  373.  
  374. _EXTERN void    Stdio_Setup _ARGS_((FILE *stream, int readable, int writable,
  375.                 unsigned char *buffer, int bufferSize,
  376.                 void (*readProc)(FILE * file),
  377.                 void (*writeProc)(FILE * file, Boolean flush),
  378.                 int (*closeProc)(FILE * file),
  379.                 ClientData clientData));
  380.  
  381. #endif /* _STDIO_H */
  382.